<html>
<head><meta charset="utf-8"><title>Can validity invariants be violated if we fix them... · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html">Can validity invariants be violated if we fix them...</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="182872739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/182872739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#182872739">(Dec 08 2019 at 02:48)</a>:</h4>
<p>... before anybody can tell?</p>
<p>So, I have a function which takes a <code>&amp;mut usize</code> which came from <code>transmute::&lt;&amp;mut NonNull&lt;u8&gt;, &amp;mut usize&gt;</code> (or the equivalent). This function needs to be unsafe, since it could cause a NonNull&lt;u8&gt; to have all-bits-zero, which is obviously illegal. I'm wondering if the constraint it has is that it must <em>leave</em> the usize as non-zero when it returns, or if the usize must <em>never</em> be zeroed.</p>
<p>Specifically, for various reasons (this is a simplified version of my actual case), it's more efficient for me to write a zero, and <em>then</em> check, rather than performing the check first, so I'd like to do that.</p>
<p>It's not clear if this is legal though. It does mean that 'memory that elsewhere in the code is represented as a NonNull&lt;T&gt;  in an invalid state' -- which on its face sounds like UB, but given that I have an &amp;mut reference to the memory in question, the NonNull shouldn't be "active" (there's probably a better term for this), and so maybe it's allowed?</p>
<p>I mocked up something with (hopefully) equivalent safety properties here: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4dcac974a42bbe70653eaed6dc11e1f6" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4dcac974a42bbe70653eaed6dc11e1f6">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4dcac974a42bbe70653eaed6dc11e1f6</a>, and miri doesn't complain, but that's not actually a guarantee, so I figured I'd ask.</p>
<p>(Note: I've simplified my actual case, which is in implementing an SSO optimized string, but I can get into details if needed, or even provide code closer to my actual example)</p>



<a name="182884330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/182884330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#182884330">(Dec 08 2019 at 09:00)</a>:</h4>
<p>validity is LLVM-level invariant</p>



<a name="182884332"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/182884332" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#182884332">(Dec 08 2019 at 09:00)</a>:</h4>
<p>safety is Rust API-level invariant</p>



<a name="182884473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/182884473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#182884473">(Dec 08 2019 at 09:05)</a>:</h4>
<p>Memory is not typed in Rust. A region of memory has to fulfill the validity invariant for <code>T</code>  when (vaguely speaking) something looks at that memory as a <code>T</code>, not at any other times. So your code is fine AFAICT.</p>



<a name="183182584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183182584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183182584">(Dec 11 2019 at 17:56)</a>:</h4>
<blockquote>
<p>I'm wondering if the constraint it has is that it must leave the usize as non-zero when it returns, or if the usize must never be zeroed.</p>
</blockquote>
<p>Very good questions! We have an open issue about this: <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/84" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/84">https://github.com/rust-lang/unsafe-code-guidelines/issues/84</a></p>



<a name="183182625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183182625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183182625">(Dec 11 2019 at 17:56)</a>:</h4>
<p><span class="user-mention" data-user-id="209168">@Thom Chiovoloni</span> could you add your usecase in that issue? I think so far we didnt really have one.</p>



<a name="183182646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183182646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183182646">(Dec 11 2019 at 17:57)</a>:</h4>
<p>We could in principle decide either way, though I see no good way to actually precisely specify things in a way that your code is <em>not</em> legal.</p>



<a name="183182662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183182662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183182662">(Dec 11 2019 at 17:57)</a>:</h4>
<p>(of course you need to be careful with panics, but that is a separate concern)</p>



<a name="183228797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183228797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183228797">(Dec 12 2019 at 04:50)</a>:</h4>
<p>@RalfJ sorry, I missed this. I'll add a comment to that issue now. Also, good catch, there was a case where a panic was in-principal possible which I hadn't considered. the code looked like:</p>
<div class="codehilite"><pre><span></span>// secretively read from our first field to see if that was the final byte
// needed to zero it out..
if unlikely!(*(self as *const _ as *const usize) == 0) {
    // oh no! overwrite it with something else quickly then move the string
    // onto heap allocated storage.
    *end = 1;
    self.handle_degenerate_push(c);
    return
}
</pre></div>


<p>but the unlikely macro is secretive, in that if <code>cfg(count_branch_hints)</code> is enabled, it records how many times it's taken/missed. that code could have probably paniced! (that said, that's a internal thing i'm doing for profiling, which end users aren't intended to enable).</p>
<p>i think it's probably possible to manage to use some bitwise int trickery to fold the equivalent to <code>|| c == '\0'</code> into a slow-path check earlier in the function, which would mean i could be certain this code never has to deal with writing a null, and both eliminate this test and the 'temporary invalid state'. (that said, i still think it's better in general if, as others put it, rust doesn't have typed memory -- in general that makes this kind of unsafe much easier to reason about than in c++ -- the lack of strict-aliasing is &lt;3)</p>



<a name="183231655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183231655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183231655">(Dec 12 2019 at 06:16)</a>:</h4>
<p>posted, but i'm sorry it's so long (i tried to shorten it ended up longer, and with some parts moved into footnotes...).</p>
<p>it should give an idea of what i want to do though. even if i fix this issue, essentially i'd like to be able to interpret any memory as any other type of memory, given the constraints of:</p>
<ul>
<li>references and dereferenced pointers always have accessed suitable alignment</li>
<li>reference mutability and aliasing rules are <em>always</em> respected </li>
<li>validity requirements are <em>ultimately</em> respected for any variable copied/read from/???, but might be very temporarily disrespected while the value is being interpreted (realistically, via a mut borrow) as a different kind of memory.</li>
</ul>
<p>The last bit is the relevant one here, and it's error prone, but in hot code my experience has often been that it can be much faster to 'do then check' vs 'check than do' -- the 2nd case has to check both if the thing we're considering doing could cause a problem (for any state -- e.g. c == '\0'), and that we're currently in a state where it would cause a problem for us (e.g. len == 7 &amp;&amp; first 7 bytes were also NUL -- for a 64 bit system)</p>
<p>And so i'm glad to hear you don't think there's a reason to disallow it. (now, there are plenty of things i'd like to do that are even dodgier, but that are disallowed, but i guess i can wait until inline assembly stabilizes to, say, read a little past the end of objects for cases where i know a page fault is impossible... :p)</p>



<a name="183254337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254337">(Dec 12 2019 at 12:36)</a>:</h4>
<p><span class="user-mention" data-user-id="209168">@Thom Chiovoloni</span> you assume that you can't use an union or an enum</p>



<a name="183254344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254344">(Dec 12 2019 at 12:36)</a>:</h4>
<p>go for a struct instead</p>



<a name="183254355"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254355" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254355">(Dec 12 2019 at 12:36)</a>:</h4>
<p>that's what i did</p>



<a name="183254359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254359">(Dec 12 2019 at 12:36)</a>:</h4>
<p>run into validity issues, and your post in the ucgs asks whether these are issues or not</p>



<a name="183254366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254366">(Dec 12 2019 at 12:37)</a>:</h4>
<p>AFAICT you can just use an enum as long as you give it the right niches</p>



<a name="183254370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254370">(Dec 12 2019 at 12:37)</a>:</h4>
<p>so none of this matters</p>



<a name="183254373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254373">(Dec 12 2019 at 12:37)</a>:</h4>
<p>i actually need to read from inactive variants.</p>



<a name="183254378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254378">(Dec 12 2019 at 12:37)</a>:</h4>
<p>it is hard to tell, since you haven't actually explained what layout you'd like to have, and why you can't have it with an enum or union</p>



<a name="183254388"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254388" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254388">(Dec 12 2019 at 12:37)</a>:</h4>
<p>all variants of an union are always active</p>



<a name="183254394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254394">(Dec 12 2019 at 12:37)</a>:</h4>
<p>there is no concept of "inactive" variants for an union</p>



<a name="183254445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254445">(Dec 12 2019 at 12:38)</a>:</h4>
<p>right, a union would be possible but i'd lose NonNull niche which i deliberately want to keep</p>



<a name="183254464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254464">(Dec 12 2019 at 12:38)</a>:</h4>
<p>I don't understand why that is the case</p>



<a name="183254466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254466">(Dec 12 2019 at 12:38)</a>:</h4>
<p>you can just give the union a niche</p>



<a name="183254468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254468">(Dec 12 2019 at 12:38)</a>:</h4>
<p>and then you get it back</p>



<a name="183254472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254472">(Dec 12 2019 at 12:38)</a>:</h4>
<p>so that Option&lt;String&gt; is the same as String. i talked more about what i'm doing and the layout in the issue i was asked to post on</p>



<a name="183254474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254474">(Dec 12 2019 at 12:38)</a>:</h4>
<p>this is exactly what i'm telling you</p>



<a name="183254481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254481">(Dec 12 2019 at 12:38)</a>:</h4>
<p>hmm</p>



<a name="183254484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254484">(Dec 12 2019 at 12:39)</a>:</h4>
<p>you are assuming that this is impossible, but maybe it isn't</p>



<a name="183254493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254493">(Dec 12 2019 at 12:39)</a>:</h4>
<p>i can give a union a niche?</p>



<a name="183254504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254504">(Dec 12 2019 at 12:39)</a>:</h4>
<p>please, explain what it is that you'd actually want</p>



<a name="183254535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254535">(Dec 12 2019 at 12:39)</a>:</h4>
<blockquote>
<p>i can give a union a niche?</p>
</blockquote>
<p>Should be as simple as <code>#[rustc_layout_scalar_valid_range_start(1)]</code></p>



<a name="183254569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254569">(Dec 12 2019 at 12:40)</a>:</h4>
<p>and if it isn't, we can just allow it</p>



<a name="183254594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254594">(Dec 12 2019 at 12:40)</a>:</h4>
<p>i want <code> union { struct { NonNull&lt;u8&gt;, usize, usize }; [u8; size_of::&lt;usize&gt;() * 3] }</code> while still keeping the NonNull's niche</p>



<a name="183254596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254596">(Dec 12 2019 at 12:40)</a>:</h4>
<p>oh i'm not using nightly</p>



<a name="183254604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254604">(Dec 12 2019 at 12:40)</a>:</h4>
<p>that does not make sense</p>



<a name="183254611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254611">(Dec 12 2019 at 12:41)</a>:</h4>
<p>since the second variant first <code>size_of::&lt;usize&gt;()</code> <code>u8</code> can be null</p>



<a name="183254622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254622">(Dec 12 2019 at 12:41)</a>:</h4>
<p>(according to those types)</p>



<a name="183254639"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254639" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254639">(Dec 12 2019 at 12:41)</a>:</h4>
<p>i'm willing to ensure that the nonnull's invariant stays respected</p>



<a name="183254692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254692">(Dec 12 2019 at 12:42)</a>:</h4>
<p><a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/13#issuecomment-419140218" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/13#issuecomment-419140218">https://github.com/rust-lang/unsafe-code-guidelines/issues/13#issuecomment-419140218</a> "My personal stanza on this is that if someone really wants their union layout optimized, we should provide attributes to let them do that" that's really what i would want</p>



<a name="183254700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254700">(Dec 12 2019 at 12:42)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">union</span> <span class="nc">U</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">heap</span>: <span class="p">(</span><span class="n">NonNull</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="kt">usize</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">sso</span>: <span class="p">(</span><span class="n">NonZeroUsize</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="p">()]),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="183254702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254702">(Dec 12 2019 at 12:42)</a>:</h4>
<p>barring that, i can just maintain validity invariant explicitly</p>



<a name="183254735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254735">(Dec 12 2019 at 12:43)</a>:</h4>
<p>hmmm</p>



<a name="183254743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254743">(Dec 12 2019 at 12:43)</a>:</h4>
<p>i guess that would work</p>



<a name="183254748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254748">(Dec 12 2019 at 12:43)</a>:</h4>
<p>That union _might_ have a niche for the first <code>usize</code></p>



<a name="183254761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254761">(Dec 12 2019 at 12:43)</a>:</h4>
<p>assuming it does it would work but i'm not sure it buys me anything</p>



<a name="183254810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254810">(Dec 12 2019 at 12:44)</a>:</h4>
<p>over just treating the struct like an array when it's sso</p>



<a name="183254814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254814">(Dec 12 2019 at 12:44)</a>:</h4>
<p>I'm just giving you what you ask for, you wanted an union with a niche at null, that is it</p>



<a name="183254819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254819">(Dec 12 2019 at 12:44)</a>:</h4>
<p>I'm not sure why you can't use an <code>enum</code> though</p>



<a name="183254836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254836">(Dec 12 2019 at 12:44)</a>:</h4>
<p>I suppose you have a discriminant somewhere to tell between sso and heap</p>



<a name="183254857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254857">(Dec 12 2019 at 12:45)</a>:</h4>
<p>So you could just use an <code>enum</code>, and give that <code>enum</code> a niche</p>



<a name="183254868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254868">(Dec 12 2019 at 12:45)</a>:</h4>
<p>my layout a little more suble than that actually. i could use an enum if i had exact control over how descriminants are chosen i guess</p>



<a name="183254874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254874">(Dec 12 2019 at 12:45)</a>:</h4>
<p>err, where the bits are placed</p>



<a name="183254879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254879">(Dec 12 2019 at 12:45)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">enum</span> <span class="nc">E</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Heap</span><span class="p">(</span><span class="n">NonNull</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="kt">usize</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Sso</span><span class="p">(</span><span class="n">NonZeroUsize</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="kt">usize</span><span class="p">;</span><span class="w"> </span><span class="mi">2</span><span class="p">]),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="183254928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254928">(Dec 12 2019 at 12:46)</a>:</h4>
<p>That enum will be 32 bytes on 64-bits, because the discriminant must be stored extra</p>



<a name="183254939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254939">(Dec 12 2019 at 12:46)</a>:</h4>
<p>yeah that's a loss.</p>



<a name="183254940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254940">(Dec 12 2019 at 12:46)</a>:</h4>
<p>but you'd need to tell Rust that there is a way to store it inside the enum</p>



<a name="183254949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254949">(Dec 12 2019 at 12:46)</a>:</h4>
<p>and i mean, it's not that this can't be made to work in an enum without changing my layout</p>



<a name="183254952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254952">(Dec 12 2019 at 12:46)</a>:</h4>
<p>it could</p>



<a name="183254966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254966">(Dec 12 2019 at 12:46)</a>:</h4>
<p>i'd just lose a couple optimizations</p>



<a name="183254977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254977">(Dec 12 2019 at 12:47)</a>:</h4>
<p>as mentioned, if you are able to fit this in an union, it will be because you are storing the discriminant inline</p>



<a name="183254982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254982">(Dec 12 2019 at 12:47)</a>:</h4>
<p>you can do that for an enum to</p>



<a name="183254983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254983">(Dec 12 2019 at 12:47)</a>:</h4>
<p>it is</p>



<a name="183254987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183254987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183254987">(Dec 12 2019 at 12:47)</a>:</h4>
<p>well</p>



<a name="183255004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255004">(Dec 12 2019 at 12:47)</a>:</h4>
<p>but you haven't explained how you would do it for the union</p>



<a name="183255070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255070">(Dec 12 2019 at 12:48)</a>:</h4>
<p>there is probably a bit somewhere that's <code>1</code> for SSO and <code>0</code> for heap or viceversa</p>



<a name="183255075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255075">(Dec 12 2019 at 12:48)</a>:</h4>
<p>it is</p>



<a name="183255099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255099">(Dec 12 2019 at 12:48)</a>:</h4>
<p>So just have two types, one with a niche for 1, and one with a niche for 0</p>



<a name="183255101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255101">(Dec 12 2019 at 12:48)</a>:</h4>
<p>there are 2 bits, individually they have specific meanings (heap vs notheap, mutable vs const). small is notheap + mutable</p>



<a name="183255105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255105">(Dec 12 2019 at 12:48)</a>:</h4>
<p>and rustc should just make the enum small automatically</p>



<a name="183255147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255147">(Dec 12 2019 at 12:49)</a>:</h4>
<p>no, i have four variants, but my descriminant layout and being consitent in how the others are represented means functions only need to check 1 case generally (small vs non-small) for fast paths</p>



<a name="183255214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255214">(Dec 12 2019 at 12:50)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">enum</span> <span class="nc">E</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Heap</span><span class="p">(</span><span class="n">NonNull</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">],</span><span class="w"> </span><span class="n">TagHeap</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Sso</span><span class="p">(</span><span class="n">NonZeroUsize</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">],</span><span class="w"> </span><span class="n">TagSso</span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="183255252"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255252" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255252">(Dec 12 2019 at 12:50)</a>:</h4>
<p>And then your <code>TagHeap</code> and <code>TagSso</code> use the <code>#[rustc_layout_scalar_valid_range_start(N)]</code> and <code>#[rustc_layout_scalar_valid_range_end(N)]</code> attributes to encode their ranges of valid values</p>



<a name="183255266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255266">(Dec 12 2019 at 12:51)</a>:</h4>
<p>right, the tag bits are in the same byte as the small-string length, and they're the most/least (depends on endian) singificant bits of the heap/arc/static string capacity</p>



<a name="183255291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255291">(Dec 12 2019 at 12:51)</a>:</h4>
<p>e.g. </p>
<div class="codehilite"><pre><span></span><span class="cp">#[repr(transparent)]</span><span class="w"></span>
<span class="cp">#[rustc_layout_scalar_valid_range_start(2)]</span><span class="w"></span>
<span class="cp">#[rustc_layout_scalar_valid_range_end(255)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">TagSso</span><span class="p">(</span><span class="kt">u8</span><span class="p">);</span><span class="w"></span>
</pre></div>



<a name="183255302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255302">(Dec 12 2019 at 12:51)</a>:</h4>
<p>lol i mean, that seems very stable</p>



<a name="183255364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255364">(Dec 12 2019 at 12:52)</a>:</h4>
<p>Once you get something like that working on nightly, with an enum, then the only question would be how to make it work on stable Rust</p>



<a name="183255374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255374">(Dec 12 2019 at 12:52)</a>:</h4>
<p>why?</p>



<a name="183255385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255385">(Dec 12 2019 at 12:52)</a>:</h4>
<p>A lot of people want to be able to define their own <code>NonZeroXY</code> types, so we might just stabilize a way to do that</p>



<a name="183255408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255408">(Dec 12 2019 at 12:53)</a>:</h4>
<p>Like, <code>NonMaxU8</code> where the niche is not at zero, but at 255, for example, and well, everything in between</p>



<a name="183255412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255412">(Dec 12 2019 at 12:53)</a>:</h4>
<p>yeah but why are you saying the way i'm doing it now is wrong?</p>



<a name="183255429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255429">(Dec 12 2019 at 12:53)</a>:</h4>
<p>fwiw this is just a proof of concept and about 50% exists to prove a bet with someone who claimed rust couldn't do this sort of thing as well as c++.</p>



<a name="183255437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255437">(Dec 12 2019 at 12:53)</a>:</h4>
<p>because as you say, you actually want an enum</p>



<a name="183255445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255445">(Dec 12 2019 at 12:53)</a>:</h4>
<p>well</p>



<a name="183255490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255490">(Dec 12 2019 at 12:54)</a>:</h4>
<p>i <em>really</em> want precise control</p>



<a name="183255497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255497">(Dec 12 2019 at 12:54)</a>:</h4>
<p><em>conceptually</em> it's an enum</p>



<a name="183255509"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255509" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255509">(Dec 12 2019 at 12:54)</a>:</h4>
<p>sure, so why shouldn't you be able to tell the enum how the discriminant is stored ?</p>



<a name="183255544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255544">(Dec 12 2019 at 12:55)</a>:</h4>
<p>why should you have to drop down to a struct or an union, deal with a lot more unsafe code, validity invariants, etc. ?</p>



<a name="183255547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255547">(Dec 12 2019 at 12:55)</a>:</h4>
<p>i feel like by defining the range of the enum i'm not actually telling it how to store things though. it still is kind of up to it whetehr or not it puts it in one place or another</p>



<a name="183255566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255566">(Dec 12 2019 at 12:55)</a>:</h4>
<p>i'd feel uncomforatble having unsafe code rely on anything less than a hard guarantee of a specific layout opt. from rustc</p>



<a name="183255595"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255595" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255595">(Dec 12 2019 at 12:56)</a>:</h4>
<p>You can make your enum <code>repr(C)</code>, to get defined layout</p>



<a name="183255632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255632">(Dec 12 2019 at 12:56)</a>:</h4>
<p>you can control how many bits the discriminant of an enum takes already as well</p>



<a name="183255636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255636">(Dec 12 2019 at 12:56)</a>:</h4>
<p>that disables layout optimizations though no? turns into tagged union</p>



<a name="183255763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255763">(Dec 12 2019 at 12:58)</a>:</h4>
<p>yep, but if you do: </p>
<div class="codehilite"><pre><span></span><span class="cp">#[repr(C)]</span><span class="w"> </span><span class="k">struct</span> <span class="nc">Heap</span><span class="p">(</span><span class="n">NonNull</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="p">...);</span><span class="w"></span>
<span class="cp">#[repr(C)]</span><span class="w"> </span><span class="k">struct</span> <span class="nc">Sso</span><span class="p">(...);</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Enum</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">Heap</span><span class="p">(</span><span class="n">Heap</span><span class="p">),</span><span class="w"> </span><span class="n">Sso</span><span class="p">(</span><span class="n">Sso</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>then the layout of the variant fields is fixed</p>



<a name="183255776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255776">(Dec 12 2019 at 12:58)</a>:</h4>
<p>anyway i'm not really interested in the most clean way to write it, i think the stdlib's string (and vec+rawvec) is very clean. i'm intersted in seeing what optimizations i can make work in rust to prove a point</p>



<a name="183255819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255819">(Dec 12 2019 at 12:59)</a>:</h4>
<p>additionally i'm interested in what kinds of optimizations rust can do that c++ can't, layout optimizations are one of them, but honestly all bitpatterns in this are fully occupited</p>



<a name="183255822"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255822" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255822">(Dec 12 2019 at 12:59)</a>:</h4>
<p>Rust enums have guaranteed layout optimizations, if you have an enum with the right niches, which you can have, then it is possible for <code>Option&lt;SSoString&gt;</code> and <code>SsoString</code> to have the same size</p>



<a name="183255837"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255837" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255837">(Dec 12 2019 at 12:59)</a>:</h4>
<p>yeah. that's true for my current implementation</p>



<a name="183255845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255845">(Dec 12 2019 at 13:00)</a>:</h4>
<p>without validity issues, and with the safety of enums</p>



<a name="183255948"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255948" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255948">(Dec 12 2019 at 13:00)</a>:</h4>
<p>If you don't care about that, just write <code>struct SsoString(NonZeroU8, [u8; ...]);</code> and deal with raw bytes, but then you are kind of on your own.</p>



<a name="183255978"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183255978" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183255978">(Dec 12 2019 at 13:01)</a>:</h4>
<p>i don't have validity issues according to everybody else who's said anything about this. and i could remove the case that's dodgy at the cost of an extra test if it turns out that it's not leglal</p>



<a name="183256068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183256068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183256068">(Dec 12 2019 at 13:02)</a>:</h4>
<p>i mean literally my string is</p>
<div class="codehilite"><pre><span></span>#[repr(C)]
pub(crate) struct String {
    data: NonNull&lt;u8&gt;,
    len: usize,
    cap_flags: CapFlags,
}
</pre></div>


<p>as i said in the issue. when it's small i just treat differently</p>



<a name="183256142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183256142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183256142">(Dec 12 2019 at 13:03)</a>:</h4>
<p>As I said, you can do that, in the same way that you can treat all your types as a <code>[u8; N]</code> buffer and disable all type safety.</p>



<a name="183256200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183256200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183256200">(Dec 12 2019 at 13:04)</a>:</h4>
<p>yeah, i mean i didn't say it was great, it's also not actually an issue for most of the code</p>



<a name="183256208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183256208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183256208">(Dec 12 2019 at 13:04)</a>:</h4>
<p>But to me the use case you posted read like an XY problem. You have an enum, that you don't know how to express in Rust. AFAICT it is possible to express it in Rust, so the usecase itself isn't that interesting to me at least, like, you should just be able to use an enum instead.</p>



<a name="183256294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183256294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183256294">(Dec 12 2019 at 13:05)</a>:</h4>
<p>yeah i can tell you think it's an XY problem. honestly, it's obvious you think i don't know what i'm doing :/. i've written plenty of rust, been following UCG, and done similar things in c++ many times.</p>



<a name="183256430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183256430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183256430">(Dec 12 2019 at 13:07)</a>:</h4>
<p>i agree that it's not very clean to interpret a struct as a bag of bytes. i'm also not interested in fighting with the compiler to attempt it to lay something out the way i've proven it out on paper</p>



<a name="183256449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183256449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183256449">(Dec 12 2019 at 13:07)</a>:</h4>
<p>you don't need to talk about SSO at all to ask whether <code>transmute::&lt;&amp;mut NonNull&lt;u8&gt;, &amp;mut usize&gt;</code> allows you to write zero through the <code>&amp;mut usize</code> or not</p>



<a name="183256504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183256504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183256504">(Dec 12 2019 at 13:08)</a>:</h4>
<p>honestly it was an attempt at heading off suggestions of XY problems :sigh:</p>



<a name="183256520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183256520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183256520">(Dec 12 2019 at 13:08)</a>:</h4>
<p>but also to give context why it might be useful <em>for my case</em></p>



<a name="183256731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183256731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183256731">(Dec 12 2019 at 13:11)</a>:</h4>
<p>anyway since i have you here, re:mimallocator (i'm the one that filed the big pr that you don't want to review lol), i could reduce the size of the PR by doing separate things for the heap-local api and the global one, but i'm not sure that would actually make "does this api seem reasonable" any easier to answer.</p>



<a name="183256788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183256788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183256788">(Dec 12 2019 at 13:12)</a>:</h4>
<p>(also if you want to talk in PM or another channel that might better, i guess, )</p>



<a name="183537741"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183537741" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183537741">(Dec 16 2019 at 10:45)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> rustc_layout_scalar_calid_range_start is for internal use only. It's perma-unstable</p>



<a name="183537768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183537768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183537768">(Dec 16 2019 at 10:45)</a>:</h4>
<p>so?</p>



<a name="183537842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183537842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183537842">(Dec 16 2019 at 10:46)</a>:</h4>
<p>Its the only way to specify niches for scalar values</p>



<a name="183537854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183537854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183537854">(Dec 16 2019 at 10:46)</a>:</h4>
<p>If you want a NonMaxU32 its the only feature you can use</p>



<a name="183537860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183537860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183537860">(Dec 16 2019 at 10:46)</a>:</h4>
<p>and that's a very reasonable thing to want</p>



<a name="183537889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183537889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183537889">(Dec 16 2019 at 10:47)</a>:</h4>
<p>I find the Small String optimization is also a very reasonable use case of user-defined niches</p>



<a name="183538069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538069">(Dec 16 2019 at 10:49)</a>:</h4>
<p>And I find it more reasonable to use that feature than to workaround it using completely illogical types to try to approximate the same effect</p>



<a name="183538146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538146">(Dec 16 2019 at 10:50)</a>:</h4>
<p>You should choose types that give meaning to your bits in memory, and not puzzle types with wrong meanings to get some particular combinations of niches, and then unsafe-code your way out of that situation</p>



<a name="183538470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538470">(Dec 16 2019 at 10:55)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> I don't think the attribute should be recommended for general use. We might change how it behaves at any time or even remove it entirely</p>



<a name="183538535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538535">(Dec 16 2019 at 10:56)</a>:</h4>
<p>I'm not recommending it for general use, I'm pointing out that it is the best tool we currently have for the job</p>



<a name="183538545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538545">(Dec 16 2019 at 10:56)</a>:</h4>
<p>If you don't agree with that, suggest a better tool</p>



<a name="183538591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538591">(Dec 16 2019 at 10:57)</a>:</h4>
<p>Worse tools had already been suggested</p>



<a name="183538596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538596">(Dec 16 2019 at 10:57)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> I would be happy if you just noted that it was perma-unstable</p>



<a name="183538607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538607">(Dec 16 2019 at 10:57)</a>:</h4>
<p>When mentioning it</p>



<a name="183538634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538634">(Dec 16 2019 at 10:57)</a>:</h4>
<p>So if we remove it, how will all NonZero / NonNull types work ?</p>



<a name="183538693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538693">(Dec 16 2019 at 10:58)</a>:</h4>
<p>Do all of them become lang items? Or does this tool get replaced with a different one ?</p>



<a name="183538730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538730">(Dec 16 2019 at 10:58)</a>:</h4>
<p>I don't see any mid term (~4 years) future were this tool is replaced with something less powerful</p>



<a name="183538765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538765">(Dec 16 2019 at 10:59)</a>:</h4>
<p>I'm not saying it will be changed because there is no need to from a compiler perspective</p>



<a name="183538785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538785">(Dec 16 2019 at 10:59)</a>:</h4>
<p>And well its unstable, and requires the rustc_attrs features, so those using it already know that it isn't stable, and rustc-only</p>



<a name="183538980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183538980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183538980">(Dec 16 2019 at 11:02)</a>:</h4>
<p>Well if you want to take the risk of an attribute which we are free to remove or fundamentally change then by all means.</p>



<a name="183539122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183539122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183539122">(Dec 16 2019 at 11:04)</a>:</h4>
<p>I mean, that's technically true for all nightly features / attributes.</p>



<a name="183539154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183539154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183539154">(Dec 16 2019 at 11:05)</a>:</h4>
<p>No matter how far a nightly feature is on the path towards stabilization, before its stabilized a 180 turn can always happen.</p>



<a name="183539172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183539172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183539172">(Dec 16 2019 at 11:05)</a>:</h4>
<p>Like, why do we have <code>specialization</code> and not <code>rustc_specialization</code></p>



<a name="183539185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183539185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183539185">(Dec 16 2019 at 11:05)</a>:</h4>
<p>A lot of nightly crates use that feature, and chances are that if the feature ever sees more progress, most of them will break.</p>



<a name="183539401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183539401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183539401">(Dec 16 2019 at 11:08)</a>:</h4>
<p>Technically yes but in practice no. Specialisation is unstable for very particular reasons whereas rustc_attrs features are never intended to be stabilised</p>



<a name="183539440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183539440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183539440">(Dec 16 2019 at 11:09)</a>:</h4>
<p>If a feature isn't stable, we can break it, and that's a risk. But trying to quantify how big is the risk in using the different nightly features in some code is quite hard. Some people think that the closer the feature is to stabilization the lesser the risk. Yet for my code, the closer a feature is to stabilization, the higher the chances that it gets iterated, and breaks.</p>



<a name="183539915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183539915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183539915">(Dec 16 2019 at 11:16)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> all I really want is that it be mentioned that a feature is perma-unstable to give users a fair warning, that's all</p>



<a name="183539935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183539935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183539935">(Dec 16 2019 at 11:17)</a>:</h4>
<p>I think I'm going to exit this conversation for now</p>



<a name="183539948"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183539948" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183539948">(Dec 16 2019 at 11:17)</a>:</h4>
<p><span class="user-mention" data-user-id="209168">@Thom Chiovoloni</span> this feature is perma-unstable, which means that there is no path for it to stabilization</p>



<a name="183540065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183540065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183540065">(Dec 16 2019 at 11:19)</a>:</h4>
<p>Its been available for years, it hasn't changed at all, there aren't AFAIK any plans to change it either, and it is required to support stable functionality that cannot be removed, which means that if this feature is removed, it needs to be replaced with something else that is at least able to support the current stable functionality, yet might not be able to support current unstable use cases (like the SSO), so apply good judgement if you decide to use it.</p>



<a name="183567072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183567072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183567072">(Dec 16 2019 at 16:45)</a>:</h4>
<p><span class="user-mention" data-user-id="126931">@centril</span>  Why is it permanently unstable? It seems extremely useful. I understand it would need an RFC and such, but is there any other reason to not make it available to the public?</p>



<a name="183571450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183571450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183571450">(Dec 16 2019 at 17:29)</a>:</h4>
<p>Yeah, I was familiar with the attribute before it was mentioned and was aware it was perma-unstable.</p>



<a name="183573747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183573747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183573747">(Dec 16 2019 at 17:57)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> because the form it exists in is very suboptimal and const generics is necessary to provide something good</p>



<a name="183594760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183594760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183594760">(Dec 16 2019 at 21:50)</a>:</h4>
<p><span class="user-mention" data-user-id="126931">@centril</span> i thought about it a bit, and i'm not sure const generics suffices to provide something good</p>



<a name="183594814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183594814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183594814">(Dec 16 2019 at 21:51)</a>:</h4>
<p>If we want to say that an integer only is valid in a particular range, we can emulate a quite good wrapper with const generics (e.g. <code>struct Int&lt;T: Int, const From: T, const To: T&gt;(T);</code>), but we can't give the type a niche with const generics I think</p>



<a name="183594825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183594825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183594825">(Dec 16 2019 at 21:51)</a>:</h4>
<p>Or at least I wouldn't know how.</p>



<a name="183594973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183594973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183594973">(Dec 16 2019 at 21:52)</a>:</h4>
<p>we'd need to be able to specify that the <code>T</code> in that wrapper only takes values in range <code>[From; To)</code>, and that feels quite a bit more like full-fledged dependent types</p>



<a name="183595405"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183595405" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183595405">(Dec 16 2019 at 21:57)</a>:</h4>
<p>I don't think that generics need to be involved at all to make it a useful ability to have, but I suppose I see why would want to do nothing in the hope that you don't trip over a forward compatibility problem.</p>



<a name="183623159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183623159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183623159">(Dec 17 2019 at 06:56)</a>:</h4>
<p>With <code>NonZeroU32</code>, you have <code>fn new(n: u32) -&gt; Option&lt;NonZeroU32&gt;</code>, which does a runtime check, and <code>unsafe fn new_unchecked(n: u32) -&gt; NonZeroU32</code>, which omits it at the cost of unsafety.  No reason you can't do the same thing for <span class="user-mention" data-user-id="132920">@gnzlbg</span>'s <code>Int</code>.</p>



<a name="183623166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183623166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183623166">(Dec 17 2019 at 06:56)</a>:</h4>
<p>What dependent types would give you is the ability to <em>safely</em> skip the runtime check in more situations.</p>



<a name="183623171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183623171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183623171">(Dec 17 2019 at 06:56)</a>:</h4>
<p>(fixed)</p>



<a name="183623193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183623193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183623193">(Dec 17 2019 at 06:57)</a>:</h4>
<p>Which I personally think would be quite nice, but that's hardly necessary if we just want to replicate the API of <code>NonZeroU32</code> and friends in a way that lets you specify any range.</p>



<a name="183640130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183640130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183640130">(Dec 17 2019 at 11:39)</a>:</h4>
<p><span class="user-mention" data-user-id="198590">@comex</span> I wasn't worried about avoiding run-time checks, what the use cases discussed here require is for <code>Option&lt;T&gt;</code> to have the same size as <code>T</code>, which is something that the const generics solution by itself doesn't give you, because <code>Int(T)</code> has the same niches as <code>T</code>, which for <code>i32</code> would be none.</p>



<a name="183640210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183640210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183640210">(Dec 17 2019 at 11:40)</a>:</h4>
<p>What I claimed is that if we wanted to give <code>struct Int&lt;T, const From: T, const To: usize&gt;(T);</code> niches outside <code>[From, To)</code>, we'd need something more than just "const generics", e.g., something that makes it UB to materialize a value of type <code>Int</code> with bit-patterns outside that range. The current attributes are a bad way of doing that, because you can't pass them the <code>From</code> and <code>To</code> parameters, but if we want to make this some type level condition in which these parameters can be used, the only-thing that comes to mind is some kind of <code>where Self.0 &gt;= From &amp;&amp; Self.0 &lt; To</code> type-level clause on the values that <code>Self</code> can take, and that sounds just like dependent types.</p>



<a name="183671763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183671763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183671763">(Dec 17 2019 at 17:40)</a>:</h4>
<p>I don't believe anyone proposes such a radical and general mechanism (at least, not specifically for replacing <code>scalar_valid_range</code>), one idea I've seen floated around is that there would would be a lang item or something that looks more or less like <code>Int</code> and has a scalar valid range defined by the values of its two const parameters.</p>



<a name="183679932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183679932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183679932">(Dec 17 2019 at 19:06)</a>:</h4>
<p>Yeah I think for most purposes people need only the simple version. In fact just having "NonMaxU32" and similar would often be enough</p>



<a name="183690078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183690078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183690078">(Dec 17 2019 at 20:51)</a>:</h4>
<p>Yeah, I was assuming it would just be a lang item.</p>



<a name="183758228"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183758228" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183758228">(Dec 18 2019 at 15:10)</a>:</h4>
<p>There is a library called <a href="https://docs.rs/optional/0.5.0/optional/" target="_blank" title="https://docs.rs/optional/0.5.0/optional/">optional</a> that lets the user customize the niche of a <code>T</code> by allowing users to specify a <code>Noned</code> bit-pattern (a niche)</p>



<a name="183758311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183758311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183758311">(Dec 18 2019 at 15:11)</a>:</h4>
<p>So you can, for example, implement <code>Noned</code> for <code>struct U32(u32);</code> and give it <code>42</code> as the <code>None</code> bit-pattern.</p>



<a name="183758642"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183758642" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183758642">(Dec 18 2019 at 15:15)</a>:</h4>
<p>I've no idea if the general problem of allowing users to specify niches on, e.g., the <code>U32</code> type itself is a problem worth solving. but at least as currently written, this approach does not support specifying an invalid bit-pattern as the <code>None</code> one (cc <span class="user-mention" data-user-id="248599">@llogiq</span> ), since the <a href="https://github.com/llogiq/optional/blob/master/src/lib.rs#L1371" target="_blank" title="https://github.com/llogiq/optional/blob/master/src/lib.rs#L1371"><code>Optional&lt;T&gt;</code></a> type actually stores a <code>T</code>, and thus the "None" bit pattern must be valid for T.</p>



<a name="183758716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183758716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183758716">(Dec 18 2019 at 15:16)</a>:</h4>
<p>The API constraints users to pick a valid value of <code>T</code> as the <code>None</code> value, so this value must be valid.</p>



<a name="183758779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/183758779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#183758779">(Dec 18 2019 at 15:17)</a>:</h4>
<p>I've seen similar types being written in C++, so apparently it appears to be something that some users want (e.g. see <a href="https://akrzemi1.wordpress.com/2015/07/15/efficient-optional-values/" target="_blank" title="https://akrzemi1.wordpress.com/2015/07/15/efficient-optional-values/">https://akrzemi1.wordpress.com/2015/07/15/efficient-optional-values/</a>).</p>



<a name="184272959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/184272959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#184272959">(Dec 26 2019 at 17:34)</a>:</h4>
<p><span class="user-mention" data-user-id="209168">@Thom Chiovoloni</span> thanks; I think that kind of code is a good argument for leaving memory untyped as I'd prefer anyway. The main concern here of course is that this will cost us some optimizations. But if C taught us anything it's that being too greedy with optimizations leads to a langauge noone can reason about...</p>



<a name="184273179"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20validity%20invariants%20be%20violated%20if%20we%20fix%20them.../near/184273179" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20validity.20invariants.20be.20violated.20if.20we.20fix.20them.2E.2E.2E.html#184273179">(Dec 26 2019 at 17:39)</a>:</h4>
<p>Sure, and those optimizations can be somewhat "made up" by leaving libraries more free to perform different optimizations, which can often have at least as substantial of an impact (if not more) than compiler-aided ones. The "typed memory" notion of C and C++ is, without a doubt, one of the biggest problems with those languages (Take a look at how many large c++ projects have -fno-strict-aliasing enabled; it's more than one might naively think, even ones with relatively high code quality like web browsers).</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>